<?php

/**
 * Fete_Controller Abstract
 * @author      Nguyen Duc Thuan<me@ndthuan.com>
 * @copyright   Nguyen Duc Thuan <me@ndthuan.com>
 */
abstract class Fete_Controller
{
    /**
     * @var Fete_View
     */
    public $view = null;

    /**
     * @var Fete_Application
     */
    protected $_app = null;

    /**
     *
     * @var Fete_Request
     */
    protected $_request = null;

    /**
     * @param Fete_Application
     * @return Fete_Controller
     */
    public function &setApplication(Fete_Application $application)
    {
        $this->_app = $application;

        return $this;
    }

    /**
     *
     * @param Fete_View $view
     * @return Fete_Controller
     */
    public function &setView(Fete_View $view)
    {
        $this->view = $view;

        return $this;
    }

    public function init()
    {}

    public function cleanUp()
    {}

    /**
     *
     * @param Fete_Request $request
     * @return Fete_Controller
     */
    public function &setRequest(Fete_Request $request)
    {
        $this->_request = $request;
        return $this;
    }

    /**
     *
     * @return Fete_Request
     */
    public function getRequest()
    {
        return $this->_request;
    }

    protected function _getParam($name)
    {
        return $this->_request->getParam($name);
    }

    protected function _getAllParams()
    {
        return $this->_request->getAllParams();
    }

    protected function _forward($action, $controller = null,
        $application = null, $params = null)
    {

        $this->_request->setAction($action);

        if (null !== $controller) {
            $this->_request->setController($controller);
        }

        if (null !== $application) {
            $this->_request->setApp($application);
        }

        if (null !== $params) {
            $this->_request->setParams($params);
        }

        $this->_app->stopRendering();
        Fete_Kernel::getInstance()->setDispatched(false);
    }

    protected function _redirect($url)
    {
        header('Location: ' . $url);
        exit;
    }
}
